********************************************************************************************
**** TREATTEST by GROUPS
**** This program compares means and equality of means
********************************************************************************************

	capt prog drop treattest_g

	program treattest_g, eclass

	syntax varlist [if] [in], [ * ]
	*marksample touse
	*markout `touse'
	tempname ivm_coef_c_f ivm_d_se_c_f ivm_d_p_c_f cm_coef_c_f cm_d_se_c_f cm_d_p_c_f Aivm_coef_c_f Aivm_d_se_c_f Aivm_d_p_c_f Acm_coef_c_f Acm_d_se_c_f Acm_d_p_c_f eth_ivm_coef_c_f eth_ivm_d_se_c_f eth_ivm_d_p_c_f eth_cm_coef_c_f eth_cm_d_se_c_f eth_cm_d_p_c_f eth_Aivm_coef_c_f eth_Aivm_d_se_c_f eth_Aivm_d_p_c_f eth_Acm_coef_c_f eth_Acm_d_se_c_f eth_Acm_d_p_c_f obs                       
                           
	foreach var of local varlist {
				
		***********************************
		**** HOUSEHOLD STRUCTURE
		***********************************
		
		* All
		
		qui reg `var' mother $controls if round == 2012, cluster(id_mun) 
		
			matrix b = e(b)
			matrix v = e(V)
			mat `cm_coef_c_f' = nullmat(`cm_coef_c_f'), b[1,1]
			mat `cm_d_se_c_f' = nullmat(`cm_d_se_c_f'), sqrt(v[1,1])
			mat `cm_d_p_c_f' = nullmat(`cm_d_p_c_f'), 2*ttail(e(df_r),abs(b[1,1]/sqrt(v[1,1])))
			mat `obs' = nullmat(`obs'), e(N)	
						
		qui treatreg `var' $controls if round == 2012, treat(mother_t = mother $controls) cluster(id_mun)
		
			matrix b = e(b)
			matrix v = e(V)
			mat `ivm_coef_c_f' = nullmat(`ivm_coef_c_f'), _b[mother_t]
			mat `ivm_d_se_c_f' = nullmat(`ivm_d_se_c_f'), _se[mother_t]
			mat `ivm_d_p_c_f' = nullmat(`ivm_d_p_c_f'), 2*ttail(e(N)-e(df_m)-1,abs(_b[mother_t]/_se[mother_t]))
			mat `obs' = nullmat(`obs'), e(N)	
		
		* Only partners 
				
		qui reg `var' mother $controls if round == 2012 & A1A2 == 1, cluster(id_mun) 
		
			matrix b = e(b)
			matrix v = e(V)
			mat `Acm_coef_c_f' = nullmat(`Acm_coef_c_f'), b[1,1]
			mat `Acm_d_se_c_f' = nullmat(`Acm_d_se_c_f'), sqrt(v[1,1])
			mat `Acm_d_p_c_f' = nullmat(`Acm_d_p_c_f'), 2*ttail(e(df_r),abs(b[1,1]/sqrt(v[1,1])))
			mat `obs' = nullmat(`obs'), e(N)	
						
		qui treatreg `var' $controls if round == 2012 & A1A2 == 1, treat(mother_t = mother $controls) cluster(id_mun)
		
			matrix b = e(b)
			matrix v = e(V)
			mat `Aivm_coef_c_f' = nullmat(`Aivm_coef_c_f'), _b[mother_t]
			mat `Aivm_d_se_c_f' = nullmat(`Aivm_d_se_c_f'), _se[mother_t]
			mat `Aivm_d_p_c_f' = nullmat(`Aivm_d_p_c_f'), 2*ttail(e(N)-e(df_m)-1,abs(_b[mother_t]/_se[mother_t]))
			mat `obs' = nullmat(`obs'), e(N)
						
		***********************************
		**** RELIGION
		***********************************
		
		
		* MUSLIM
		
			reg `var' mother $controls if round == 2012 & rel_muslim == 1, cluster(id_mun) 
			
				matrix b = e(b)
				matrix v = e(V)
				mat `eth_cm_coef_c_f' = nullmat(`eth_cm_coef_c_f'), b[1,1]
				mat `eth_cm_d_se_c_f' = nullmat(`eth_cm_d_se_c_f'), sqrt(v[1,1])
				mat `eth_cm_d_p_c_f' = nullmat(`eth_cm_d_p_c_f'), 2*ttail(e(df_r),abs(b[1,1]/sqrt(v[1,1])))
				mat `obs' = nullmat(`obs'), e(N)	
							
			treatreg `var' $controls if round == 2012 & rel_muslim == 1, treat(mother_t = mother $controls) cluster(id_mun)
			
				matrix b = e(b)
				matrix v = e(V)
				mat `eth_ivm_coef_c_f' = nullmat(`eth_ivm_coef_c_f'), _b[mother_t]
				mat `eth_ivm_d_se_c_f' = nullmat(`eth_ivm_d_se_c_f'), _se[mother_t]
				mat `eth_ivm_d_p_c_f' = nullmat(`eth_ivm_d_p_c_f'), 2*ttail(e(N)-e(df_m)-1,abs(_b[mother_t]/_se[mother_t]))
				mat `obs' = nullmat(`obs'), e(N)	
			
		* NON MUSLIM
				
			reg `var' mother $controls if round == 2012 & rel_muslim == 0, cluster(id_mun) 
			
				matrix b = e(b)
				matrix v = e(V)
				mat `eth_Acm_coef_c_f' = nullmat(`eth_Acm_coef_c_f'), b[1,1]
				mat `eth_Acm_d_se_c_f' = nullmat(`eth_Acm_d_se_c_f'), sqrt(v[1,1])
				mat `eth_Acm_d_p_c_f' = nullmat(`eth_Acm_d_p_c_f'), 2*ttail(e(df_r),abs(b[1,1]/sqrt(v[1,1])))
				mat `obs' = nullmat(`obs'), e(N)	
							
			treatreg `var' $controls if round == 2012 & rel_muslim == 0, treat(mother_t = mother $controls) cluster(id_mun)
			
				matrix b = e(b)
				matrix v = e(V)
				mat `eth_Aivm_coef_c_f' = nullmat(`eth_Aivm_coef_c_f'), _b[mother_t]
				mat `eth_Aivm_d_se_c_f' = nullmat(`eth_Aivm_d_se_c_f'), _se[mother_t]
				mat `eth_Aivm_d_p_c_f' = nullmat(`eth_Aivm_d_p_c_f'), 2*ttail(e(N)-e(df_m)-1,abs(_b[mother_t]/_se[mother_t]))
				mat `obs' = nullmat(`obs'), e(N)					
						
	}
		
	foreach mat in ivm_coef_c_f ivm_d_se_c_f ivm_d_p_c_f cm_coef_c_f cm_d_se_c_f cm_d_p_c_f Aivm_coef_c_f Aivm_d_se_c_f Aivm_d_p_c_f Acm_coef_c_f Acm_d_se_c_f Acm_d_p_c_f eth_ivm_coef_c_f eth_ivm_d_se_c_f eth_ivm_d_p_c_f eth_cm_coef_c_f eth_cm_d_se_c_f eth_cm_d_p_c_f eth_Aivm_coef_c_f eth_Aivm_d_se_c_f eth_Aivm_d_p_c_f eth_Acm_coef_c_f eth_Acm_d_se_c_f eth_Acm_d_p_c_f {
		mat coln ``mat'' = `varlist'
	}

	tempname b V
	mat `b' = `ivm_coef_c_f'*0
	mat `V' = `b''*`b'
	eret post `b' `V'
	eret local cmd "treattest_g"
	
	mat temp = `obs'
	
	* Household structure
		eret scalar N1 = temp[1,1] // Full sample
		eret scalar N2 = temp[1,2] // Full sample IV
		eret scalar N3 = temp[1,3] // Only partners 
		eret scalar N4 = temp[1,4] // Only partners IV
	*Religion
		eret scalar N5 = temp[1,5] // Full sample
		eret scalar N6 = temp[1,6] // Full sample IV
		eret scalar N7 = temp[1,7] // Only partners 
		eret scalar N8 = temp[1,8] // Only partners IV
				
	foreach mat in ivm_coef_c_f ivm_d_se_c_f ivm_d_p_c_f cm_coef_c_f cm_d_se_c_f cm_d_p_c_f Aivm_coef_c_f Aivm_d_se_c_f Aivm_d_p_c_f Acm_coef_c_f Acm_d_se_c_f Acm_d_p_c_f eth_ivm_coef_c_f eth_ivm_d_se_c_f eth_ivm_d_p_c_f eth_cm_coef_c_f eth_cm_d_se_c_f eth_cm_d_p_c_f eth_Aivm_coef_c_f eth_Aivm_d_se_c_f eth_Aivm_d_p_c_f eth_Acm_coef_c_f eth_Acm_d_se_c_f eth_Acm_d_p_c_f obs {
		eret mat `mat' = ``mat''
	}
	
	end
